//
//  OperateExten.m
//  Jyxt
//
//  Created by 刘日 on 17/5/18.
//  Copyright © 2017年 DaLian Rental Housing Management. All rights reserved.
//

#import "OperateExten.h"
#import <MJRefresh.h>

@implementation OperateExten

- (void)loadUrl:(NSString *)url{
    [self.webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]]];
}

- (void)reload{
    [self.webView reload];
}

- (void)setEnableDropRefresh:(BOOL)enableDropRefresh{
    _enableDropRefresh = enableDropRefresh;
    self.webView.scrollView.mj_header = enableDropRefresh ? [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.webView evaluateJavaScript: @"$LM.loadDelegate.dropRefreshCallback()" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
            NSLog(@"下拉刷新回调触发完毕");
        }];
    }] : nil;
}

- (void)setEnablePullUpLoading:(BOOL)enablePullUpLoading{
    _enablePullUpLoading = enablePullUpLoading;
    self.webView.scrollView.mj_footer = enablePullUpLoading ? [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self.webView evaluateJavaScript: @"$LM.loadDelegate.pullUpLoadingCallback()" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
            NSLog(@"上拉加载回调触发完毕");
        }];
    }] : nil;
}

- (void)stopDropRefresh{
    [self.webView.scrollView.mj_header endRefreshing];
}

- (void)stopPullUpLoading{
    [self.webView.scrollView.mj_footer endRefreshing];
}

- (void)evaluateJavaScript:(nonnull NSString *)js complete: (LK_JS_EXECUTE_COMPLETE)complete{
    [self.webView evaluateJavaScript: js completionHandler:^(id _Nullable data, NSError * _Nullable error) {
        complete(data , error);
    }];
}

- (NSString *)encodeToPercentEscapeString: (NSString *) input{
    NSString *outputStr = (__bridge NSString *)
    CFURLCreateStringByAddingPercentEscapes(
                                            NULL,
                                            (__bridge CFStringRef)input,NULL, /* charactersToLeaveUnescaped */(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);
    return outputStr;
    
}

@end
